#!/usr/bin/python

import re, sys, urllib2, sha, os.path
sys.path.append("/usr/local/admin")

from log import log

scriptname = 'bom.xml'

# key is state, value is a set
capitalLocations = dict()
otherLocations = dict()

# key is state, value is URL
capitalURL = dict()
otherURL = dict()

def getLocations():
    """ 
                   urlNow["Qld"] = "http://www.bom.gov.au/products/IDQ10095.shtml";
    """
    reURL = re.compile(r'.*urlNow\[\"(\w+)\"\].+\"(.+)608(\d\d)(.*)\";.*')
    for line in file(scriptname):
        matches = reURL.match(line)
        if matches is not None:
            state = matches.group(1)
            cap = "%s609%s%s" % (matches.group(2), matches.group(3), matches.group(4))
            other = "%s608%s%s" % (matches.group(2), matches.group(3), matches.group(4))
            capitalURL[state] = cap
            otherURL[state] = other
            capitalLocations[state] = set()
            otherLocations[state] = set()

    reLocation = re.compile(r'href=\"[^\"]+shtml\">([\w \-\.\']+)<\/a><\/td>')
    #   <td class="rowleftcolumn"><a href="/products/IDV60801/IDV60801.95835.shtml">Longerenong</a></td>

    for state in capitalURL.keys():
        url = capitalURL[state]
        urlcache = ".cache-" + sha.new(url).hexdigest()
        if not os.path.exists(urlcache):
            log.info("Caching %s" % url)
            urlData = urllib2.urlopen(url).read()
            file(urlcache, "w").write(urlData)
            
        for match in reLocation.finditer(file(urlcache).read()):
            capitalLocations[state].add(match.group(1))


    for state in otherURL.keys():
        url = otherURL[state]
        urlcache = ".cache-" + sha.new(url).hexdigest()
        if not os.path.exists(urlcache):
            log.info("Caching %s" % url)
            urlData = urllib2.urlopen(url).read()
            file(urlcache, "w").write(urlData)

        for match in reLocation.finditer(file(urlcache).read()):
            if not match.group(1) in capitalLocations[state]:
                otherLocations[state].add(match.group(1))

def substituteLocations():
    #     <EnumValue value="Vic|Melbourne|1" display_value="Vic - Melbourne"/>
    enumstring = ""
    data = ""
    states = capitalLocations.keys()
    states.sort()
    # capitals
    for state in states:
        locations = [x for x in capitalLocations[state]]
        locations.sort()
        for location in locations:
            data += """    <EnumValue value="%s|%s|1" display_value="%s - %s" />\n""" % (state, location, state, location)
        
    for state in states:
        locations = [x for x in otherLocations[state]]
        locations.sort()
        for location in locations:
            data += """    <EnumValue value="%s|%s|0" display_value="%s - %s" />\n""" % (state, location, state, location)
        
    print data
    


getLocations()
substituteLocations()
